home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Views / Includes / UTEView.h < prev   
Encoding:
Text File  |  1996-04-03  |  21.8 KB  |  597 lines  |  [TEXT/MPS ]

  1. // UTEView.h
  2. // Copyright © 1986-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4.  
  5. #ifndef __UTEVIEW__
  6. #define __UTEVIEW__
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UVIEW__
  11. #include "UView.h"
  12. #endif
  13.  
  14. // Toolbox
  15.  
  16. #ifndef __TEXTEDIT__
  17. #include <TextEdit.h>
  18. #endif
  19.  
  20. // ANSI
  21.  
  22. #ifndef __LIMITS__
  23. #include <limits.h>
  24. #endif
  25.  
  26. //----------------------------------------------------------------------------------------
  27. // Forward and external class declarations
  28. //----------------------------------------------------------------------------------------
  29.  
  30. class TDocument;
  31. class TEditText;
  32. class TScroller;
  33. class TTECommand;
  34. class TTEStyleCommand;
  35. class TTETypingCommand;
  36. class TToolboxEvent;
  37.  
  38.  
  39. //----------------------------------------------------------------------------------------
  40. // Constants
  41. //----------------------------------------------------------------------------------------
  42.  
  43. const unsigned short kUnlimited = SHRT_MAX;    // The maximum number of characters in the
  44.                                                 // fText of a TTEView object
  45.  
  46. const Boolean kWithStyle = TRUE;                // Parameters to TTEView::ITEView
  47. const Boolean kWithoutStyle = FALSE;
  48.  
  49. const Boolean kSaveCurrentChars = TRUE;            // Parameter to ITECommand
  50.  
  51. const unsigned char Chr00 = 0;                // Needed for ControlCharSet
  52.  
  53. const unsigned char Chr1F = 31;
  54.  
  55. const VCoordinate kMinAhead = 96;                // Default value for fMinAhead
  56.  
  57.  
  58. //----------------------------------------------------------------------------------------
  59. // Typedefs
  60. //----------------------------------------------------------------------------------------
  61.  
  62. typedef long ControlCharSet;
  63.  
  64. //----------------------------------------------------------------------------------------
  65. // TTEView: TTEView is a view subclass representing a TextEdit record. TextEdit is the
  66. // simple text-editing facility built into the Macintosh ROM. The purpose of a TEView is
  67. // to make TextEdit function properly in a MacApp environment, for things like scrolling,
  68. // printing, page breaks, and command handling
  69. //----------------------------------------------------------------------------------------
  70.  
  71. class TTEView : public TView
  72. {
  73.     MA_DECLARE_CLASS;
  74.     
  75. public:
  76.  
  77.     //------------------------------------------------------------------------------------
  78.     // Initialize and Free
  79.     //------------------------------------------------------------------------------------
  80.  
  81.     TTEView();
  82.         // Constructor
  83.         
  84.     void ITEView(TDocument* itsDocument,
  85.                                 TView* itsSuperView,
  86.                                 const VPoint& itsLocation,
  87.                                 const VPoint& itsSize,
  88.                                 SizeDeterminer itsHDeterminer,
  89.                                 SizeDeterminer itsVDeterminer,
  90.                                 const VRect& itsInset,
  91.                                 const TextStyle& itsTextStyle,
  92.                                 short itsJustification,
  93.                                 Boolean itsStyleType,
  94.                                 Boolean itsAutoWrap);
  95.         // For Initialization of procedurally created TEView.
  96.  
  97.     virtual TObject* Clone();
  98.         // calls Inherited::Clone and then clones owned objects
  99.  
  100.     virtual ~TTEView();
  101.         // Frees the TERecord and optionally fText, then Inherited::Free.
  102.  
  103.  
  104.     //------------------------------------------------------------------------------------
  105.     // Standard signature support.
  106.     //------------------------------------------------------------------------------------
  107.  
  108.     virtual IDType GetStandardSignature();    // override 
  109.         // Returns this class's standard signature.
  110.  
  111.  
  112.     //------------------------------------------------------------------------------------
  113.     // Stream I/O protocol support.
  114.     //------------------------------------------------------------------------------------
  115.  
  116.     virtual void ReadFields(TStream* aStream);    // override 
  117.     
  118.     virtual void WriteFields(TStream* aStream);    // override 
  119.  
  120.  
  121.     //------------------------------------------------------------------------------------
  122.     // View method overrides
  123.     //------------------------------------------------------------------------------------
  124.  
  125.     virtual void MakeTERecord();
  126.         // Called with grafport properly set, to create the actual TE Record
  127.  
  128.  
  129.     //------------------------------------------------------------------------------------
  130.     //  Commands and Menus
  131.     //------------------------------------------------------------------------------------
  132.  
  133.     virtual void DoKeyEvent(TToolboxEvent* event);
  134.         // Handles keystrokes
  135.  
  136.     virtual TTECommand* DoMakeEditCommand(CommandNumber aCommandNumber);
  137.         // Make a command for handling edit menu stuff
  138.  
  139.     virtual TTEStyleCommand* DoMakeStyleCommand(const TextStyle& aStyle,
  140.                                                        CommandNumber itsCommandNumber,
  141.                                                        short itsMode);
  142.     // Make a style change command
  143.  
  144.     virtual TTETypingCommand* DoMakeTypingCommand(const CStr2& ch);
  145.         // Make a typing command for handling the new character[s]
  146.  
  147.     virtual void DoMenuCommand(CommandNumber aCommandNumber);
  148.         // Handles menu commands
  149.  
  150.     virtual void DoMouseCommand(VPoint& theMouse,
  151.                                        TToolboxEvent* event,
  152.                                        CPoint hysteresis);
  153.         // Handles mousepresses
  154.  
  155.     virtual void DoneTyping();
  156.         // No further typing can occur for the current typing command
  157.  
  158.     virtual void DoSetupMenus();
  159.         // Sets up menus
  160.  
  161.     virtual void SetActive(Boolean state);
  162.         // Calls TEActivate or TEDeactivate
  163.  
  164.     virtual void BecameWindowTarget();        // Override
  165.     
  166.     virtual void ResignedWindowTarget();        // Override
  167.     
  168.     virtual void BecameTarget();                // Override
  169.     
  170.     virtual void ResignedTarget();            // Override
  171.  
  172.     virtual void SetEnable(Boolean state);
  173.         // Resets the view's idle
  174.  
  175.     virtual TEditText* GetEditText();
  176.         // Get the edit text view, if any, that owns this TE view. 
  177.  
  178.  
  179.     //------------------------------------------------------------------------------------
  180.     //  Screen Display
  181.     //------------------------------------------------------------------------------------
  182.  
  183.     virtual Boolean DoIdle(IdlePhase phase);
  184.         // Blinks the insertion CPoint
  185.  
  186.     virtual void Draw(const VRect& area);
  187.         // Draw the text in a frame or on the printed page
  188.  
  189.     virtual void ShowReverted();
  190.         // Make sure line starts are correct before redisplay
  191.  
  192.  
  193.     //------------------------------------------------------------------------------------
  194.     //  Size Changes
  195.     //------------------------------------------------------------------------------------
  196.  
  197.     virtual void BeInPort(GrafPtr itsPort);
  198.         // Now that a grafport is established for the view, tells TextEdit about it
  199.  
  200.     virtual void BeInScroller(TScroller* itsScroller);
  201.         // Gives us a chance to set the scroll parameters
  202.  
  203.     virtual VRect CalcMinFrame();
  204.         // Compute the minimum size of the view
  205.  
  206.     virtual long CalcRealHeight();
  207.         // Calculate true height of record, including last character if it is a carriage
  208.         // return
  209.  
  210.     virtual long CalcRealWidth();
  211.         // Calculate true width of record.Only supported for NON-styled TE Records right
  212.         // now
  213.  
  214.     virtual void ComputeFrame(VRect& newFrame);
  215.         // Compute the actual size of the view
  216.  
  217.     virtual void SetFrame(const VRect& newFrame, Boolean invalidate);
  218.         // Installs the new size for both the View and TextEdit
  219.  
  220.     virtual void InvalidateFrameDifference(const VRect& oldFrame, const VRect& newFrame); // Override
  221.         // Overridden to account for fInset
  222.  
  223.     virtual void UpdateCoordinates();// override
  224.         // Keeps the TE in synch with any VSpace or QDSpace origin changes.
  225.  
  226.     virtual void SynchView(Boolean redraw);
  227.         // Keeps view metrics in synch after TextEdit operation. If redraw is true then
  228.         // ScrollSelectionIntoView is called to make sure the selection is visible.
  229.  
  230.     virtual VPoint OffsetToPt(short offset);
  231.         // Converts offset in text to view coordinates.
  232.  
  233.     virtual short OffsetToLine(short offset);
  234.         // Returns the line number that contains the offset
  235.  
  236.     virtual short OffsetToLineStart(short offset);
  237.         // Returns the offset of the start of the line that contains the offset
  238.  
  239.     virtual short OffsetToLineEnd(short offset);
  240.         // Returns the offset of the end of the line that contains the offset
  241.  
  242.     virtual short PtToOffset(const VPoint& aPoint);
  243.         // Converts a view CPoint to an offset in the text.
  244.  
  245.     virtual Boolean WordBounds(short charPos,
  246.                                       short& wordStart,
  247.                                       short& wordEnd);
  248.         // charPos is the offset of a specific character in the text Returns: true iff
  249.         // that character is part of a word wordStart = offset of first character of the
  250.         // word wordEnd= offset of final character of the word Note: these are CHARACTER
  251.         // offsets, not CURSOR POSITIONS.
  252.  
  253.  
  254.     //------------------------------------------------------------------------------------
  255.     // NOTE -- These methods apply only to a TEView installed as the view in the Clipboard
  256.     //------------------------------------------------------------------------------------
  257.  
  258.     virtual Boolean ContainsClipType(ResType aType);
  259.         // Determines whether the indicated clipboard type can be produced by the view
  260.  
  261.     virtual long GivePasteData(Handle aDataHandle,
  262.                                       ResType dataType);
  263.         // Supplies data to be PASTED by some requesting PASTE command
  264.  
  265.     virtual void WriteToDeskScrap();
  266.         // Produces TEXT data for the Desk Scrap when called upon to do so
  267.  
  268.  
  269.     //------------------------------------------------------------------------------------
  270.     //  Miscellaneous
  271.     //------------------------------------------------------------------------------------
  272.  
  273.     virtual void AutoScrolling(Boolean doScrolling);
  274.         // Set TERecord auto scrolling to given value via TEAutoScroll
  275.  
  276.     virtual Boolean ClickLoop();
  277.         // Forwarded to from the global routine: ClickLoopForTTEView which was set as the
  278.         // TE's ClickLoop.Typically handles autoscrolling for the view.
  279.  
  280.     virtual void CalcSelLoc(VRect& selectionRect);
  281.         // Calculates the location of the current Selection
  282.  
  283.     virtual void SetSelection(short NewSelStart,
  284.                                      short NewSelEnd,
  285.                                      Boolean redraw);
  286.  
  287.     virtual void GetSelectionString(CStr255& selection);
  288.         // Returns the selected text as a CStr255. If the selection is longer than 255
  289.         // characters, the first 255 characters are returned
  290.         
  291.     virtual short GetNumberOfChars();
  292.         // Returns the number of characters stored in the text handle in a script
  293.         // manager compatible way.
  294.  
  295.     short GetCharacterOffset(short fromOffset);
  296.         // Returns the offset of the beginning of the proposed offset
  297.         // Correcting for multibyte characters
  298.  
  299.     short GetBytesInCharacter(short atOffset);
  300.         // Returns the number of bytes in the character pointed
  301.  
  302.     short GetNextCharacterOffset(short fromOffset);
  303.         // Returns the offset of the character following this offset
  304.         // Correcting for multibyte characters
  305.  
  306.     short GetPrevCharacterOffset(short fromOffset);
  307.         // Returns the offset of the character before this offset
  308.         // Correcting for multibyte characters
  309.  
  310.     virtual void ChangeWrap(Boolean newAutoWrap,
  311.                                    Boolean redraw);
  312.         // Changes auto-wrapping behavior, redrawing if requested
  313.  
  314.     virtual Boolean ContinuousStyle(short firstChar,
  315.                                            short lastChar,
  316.                                            short& mode,
  317.                                            TextStyle& aStyle);
  318.         // Returns true if the style of the characters within the given range is wholly
  319.         // continuous. The style attributes to check are specified by mode, which has the
  320.         // same meaning as in the TextEdit call TESetStyle. Attributes which are
  321.         // continuous are returned in aStyle, with the appropriate bits set in mode. Font
  322.         // style attribute bits are individually checked across the selection range, and
  323.         // returned set to 1 if that attribute is continuous.
  324.  
  325.     virtual void ExtractStyles(TEStyleHandle& theStyles,
  326.                                       STHandle& theElements);
  327.         // Extract handles to style information for a TextEdit record, presumably for the
  328.         // purpose of storing the data to disk. Note that line heights can be
  329.         // recalculated, and as such don't need to be stored.
  330.  
  331.     virtual Handle ExtractText() const;
  332.         // For sake of completeness. Extract handle to text, presumably for the purpose of
  333.         // storing the text externally (perhaps to disk).
  334.  
  335.     virtual void RecalcText();
  336.         // Tells TextEdit to recompute linestarts
  337.  
  338.     virtual void ScrollSelectionIntoView(Boolean redraw);    // Override
  339.         // Scrolls the selection into view -- called after Commands are done or undone If
  340.         // auto scrolling is on, calls TESelView to do the right thing
  341.  
  342.     virtual void SetJustification(short newJust,
  343.                                          Boolean redraw);
  344.         // Sets the TERecord to the given justification. If redraw is true then the view
  345.         // is redrawn by calling ForceRedraw. Otherwise it is assumed the called will get
  346.         // the view redrawn.
  347.  
  348.     virtual void SetOneStyle(short theStart,
  349.                                     short theEnd,
  350.                                     short theMode,
  351.                                     const TextStyle& theStyle,
  352.                                     Boolean redraw);
  353.         // Sets a text style continuously over a range. In unstylish TERecords, the style
  354.         // is set across the entire record. If redraw is true then the text is redrawn.
  355.  
  356.     virtual void SetText(const CStr255& theText);
  357.         // Convenience routine, to save from having to create a handle just to pass to
  358.         // StuffText. Note that if fSavedTEHandle already holds a saved handle, it will be
  359.         // disposed before StuffText is called.
  360.  
  361.     virtual Boolean SpaceForStyles(long rangeStart,
  362.                                           long rangeEnd);
  363.         // Returns true if there is enough memory to hold the styles over the given range
  364.         // in the TERecord
  365.  
  366.     virtual void StuffStyles(TEStyleHandle theStyles,
  367.                                     STHandle theElements);
  368.         // Installs style information, presumably fetched from disk, into the TextEdit
  369.         // record.
  370.  
  371.     virtual void StuffText(Handle theText);
  372.         // Installs text into the TextEdit record. Note that this destroys any existing
  373.         // style information -- StuffStyles should be called if styles are to be attached
  374.         // to this text information
  375.  
  376.     virtual void StuffTERects(const CRect& newTERect);
  377.         // installs "newTERect" as the dest & view CRect of the TextEdit record
  378.  
  379.  
  380.     //------------------------------------------------------------------------------------
  381.     //  Printing
  382.     //------------------------------------------------------------------------------------
  383.  
  384.     virtual VCoordinate DoBreakFollowing(VHSelect vhs,
  385.                                                 VCoordinate previousBreak,
  386.                                                 Boolean& automatic);
  387.         // Computes the next page-break following 'previousBreak' in direction given by
  388.         // 'vhs'
  389.  
  390.     virtual VPoint DoCalcViewPerPage();
  391.         // Computes how much of the view is to be allocated to each printed page
  392.  
  393.     virtual void DoSetPageOffset(const VPoint& coord);
  394.         // At printing time, determine the location of the 'interior' (body) of the page
  395.  
  396.     virtual VRect GetPrintExtent();
  397.         // Print extent is the view's extent minus the insets.
  398.  
  399. #if qDrag
  400.  
  401.     //------------------------------------------------------------------------------------
  402.     //  Drag/Drop Support
  403.     //------------------------------------------------------------------------------------
  404.     
  405.     virtual RgnHandle DoMakeDragCursorRegion(); // Override
  406.         // Create and return a description of the drag region in local coordinates
  407.         
  408.     virtual Boolean WillDrag(const VPoint& theMouse, const RgnHandle dragCursorRegion); // Override
  409.         // Give a mouse point in local coordinates, returns true if clicking the mouse
  410.         // would initiate a drag operation
  411.     
  412.     virtual short GetWillDragCursorID();    // Override
  413.         // display quickdraw arrow cursor
  414.         
  415.     virtual short GetIsDraggingCursorID();    // Override
  416.         // display quickdraw arrow cursor
  417.  
  418.     virtual void DoAddDragContent(); // Override
  419.         // Install flavors into the current drag operation
  420.  
  421.     virtual Boolean WillAcceptDrop(CDragItemIterator& dragItemIterator); // Override
  422.         // Return true if the drag contains exactly one item and is available as
  423.         // text
  424.         
  425.     virtual void DoDragEnter(); // Override
  426.         // Prepare to track the drag
  427.         
  428.     virtual void DoDragWithin(const VPoint& localMouse); // Override
  429.         // Track insertion location and manage drawing the blinking caret.
  430.     
  431.     virtual void DoDragLeave(); // Override
  432.         // Clean up after being the drag target.
  433.         
  434.     virtual TCommand* DoMakeDragDropCommand(CommandNumber         itsCommandNumber,
  435.                                             CDragItemIterator&     dragItemIterator);
  436.         // Create a command to manage a drag or drop operation.
  437.  
  438.     virtual void DoFulfillPromise(TDragItem *promisedItem); // Override
  439.         // Provide promised flavor data
  440.  
  441.     virtual void DrawCaret( short offset);
  442.         // Given an offset, draw or undraw the blinking caret while a drag is being tracked.
  443.                 
  444. #endif // qDrag
  445.  
  446.  
  447. //----------------------------------------------------------------------------------------
  448. // static member functions
  449. //----------------------------------------------------------------------------------------
  450. public:
  451.     static void SetSelect(short theStart, short theEnd, TEHandle hTE);
  452.         // For those times when we don't want the selection range hilited when changed
  453.     
  454.     static pascal Boolean ClickLoopForTTEView();
  455.         // Called from TextEdit when the mouse is down forwards to TTEView::ClickLoop so that
  456.         // autoscrolling may be done.
  457.         // NOTE: this routine requires the pascal keyword because the Toolbox requires it
  458.     
  459. #if qDebug
  460.     static void DumpTERecord(TEHandle aTEH);
  461.         // writes salient information about the TERecord out to the Debug window
  462. #endif
  463.         
  464.     static short TEIsFrontOfLine(short offset, TEHandle theTE);
  465.     
  466.     static short TEGetLine(short offset, TEHandle theTE);
  467.     
  468.     static Boolean IsBlank( Handle theText, short offset, short& length );
  469.  
  470. protected:
  471. #if qDebug
  472.     static void WriteChar(short index, Handle hText);
  473. #endif
  474.     
  475. //----------------------------------------------------------------------------------------
  476. // data members
  477. //----------------------------------------------------------------------------------------
  478. public:
  479.  
  480.     TextStyle fTextStyle;                        // Current style of text
  481.  
  482.     VRect fInset;                                // Amount to inset text from edges of view
  483.  
  484.     TTETypingCommand* fTypingCommand;            // The current TE Typing command relating
  485.                                                 // to me, if any
  486.  
  487.     TEHandle fHTE;                                // Handle to the actual TextEdit object
  488.  
  489.     Handle fText;                                // The text in the TEHandle
  490.  
  491.     Handle fSavedTEHandle;                        // Saved handle from TENew
  492.  
  493.     VCoordinate fLastHeight;                    // Last checked height of record. If the
  494.                                                 // record is stylish, this represents the
  495.                                                 // height in pixels. For old-style
  496.                                                 // records, equals number of lines.
  497.  
  498.     VCoordinate fLastWidth;                        // Last checked width of record. Not
  499.                                                 // currently used If the TE is styled,
  500.                                                 // this represents the width in pixels.
  501.  
  502.     VCoordinate fMinAhead;                        // Minimum amount by which to autoscroll
  503.                                                 // ahead when scrolling selection into
  504.                                                 // view.(When typing etc.
  505.  
  506.     VCoordinate fLastPageBreak;                    // Caches last page break computed.
  507.  
  508.     CommandNumber fKeyCommandNumber;            // Will be used as the CString number for
  509.                                                 // "Undo Typing"
  510.  
  511.     ControlCharSet fControlChars;                // Control chars accepted in text
  512.  
  513.     unsigned short fMaxChars;                    // Maximum number of chars to accept into
  514.                                                 // fText. Default is SHRT_MAX; stuff to
  515.                                                 // different value if you want to limit
  516.                                                 // max chars to fewer than SHRT_MAX
  517.  
  518.     ResNumber fTextStyleRsrcID;                    // Rsrc ID of 'TxSt' resource which has
  519.                                                 // TextStyle information.
  520.  
  521.     short fJustification;                        // Current justification of text record
  522.  
  523.     unsigned short fLastLine;                    // Last line of text of the last page
  524.                                                 // break
  525.  
  526.     short fSelAnchor;                            // current selection "anchor"
  527.  
  528.     short fUpDownH;                                // for repeated Up- and/or Down-arrowing
  529.     
  530.     Boolean fPreferOutline;                        // prefer outline fonts?
  531.  
  532.     Boolean fAcceptsChanges;                    // Set to false to have text which will
  533.                                                 // not accept any change, such as text on
  534.                                                 // the Clipboard, or perhaps received mail
  535.  
  536.     Boolean fStyleType;                            // Set to kWithStyle if record is styled
  537.  
  538.     Boolean fAutoWrap;                            // Set to false for line wrapping at CR's
  539.                                                 // only
  540.  
  541.     Boolean fFreeText;                            // Determines if fText should be freed on
  542.                                                 // Free.
  543.  
  544.     Boolean fSpecsChanged;                        // Something recently happened which could
  545.                                                 // effect font/style/size/ color menu item
  546.                                                 // updating. Should be reset to false when
  547.                                                 // application has taken the appropriate
  548.                                                 // action.
  549.  
  550.     Boolean fUpDown;                            // used to keep track of horizontal
  551.                                                 // position
  552.  
  553.     //------------------------------------------------------------------------------------
  554.     //  Drag and Drop Data
  555.     //------------------------------------------------------------------------------------
  556. #if qDrag
  557. public:
  558.     RgnHandle fHiliteRgn;
  559.  
  560.     unsigned long fLastCaretTime;
  561.     
  562.     short fLastCaretOffset;
  563.     
  564.     Boolean fCaretIsShown;
  565.     
  566. #endif
  567.  
  568. //----------------------------------------------------------------------------------------
  569. // static data members
  570. //----------------------------------------------------------------------------------------
  571. public:
  572.     static TEClickLoopUPP fgDefClickLoopProc;                // Standard TextEdit click loop routine
  573.     
  574.     // The following are considered private, but appear in the interface in case you need to
  575.     // override a method that uses one of these.
  576.     static TTEView* fgCurrTEView;                            //For the benefit of ClickLoopForTTEView only
  577.     
  578.     static TEClickLoopUPP fgClickLoopProc;
  579.         // The fgClickLoopProc global is used for PPC compatibility.  In the case of a
  580.         // PPC application, we create a single UniversalProcPtr for ClickLoopForTTEView.
  581.  
  582.     #if qDebug
  583.     static Boolean fgTEIntenseDebugging;
  584.     #endif
  585. };
  586.  
  587. //----------------------------------------------------------------------------------------
  588. // Global procedure declarations
  589. //----------------------------------------------------------------------------------------
  590.  
  591. void InitUTEView();
  592.     // Initialize TEView unit.
  593.  
  594. #endif
  595.  
  596.  
  597.